$(function () {
    let id = location.search.split('?')[1];
    if (id) {
        $.get('http://localhost:8000/product', data => {
            let newData = data.filter(item => {
                return item.id == id
            })
            $('[name=id]').val(newData[0].id)
            $('[name=name]').val(newData[0].name)
            $('[name=age]').val(newData[0].age)
        })
    }
})

function save() {
    let obj = {
        id:$('[name=id]').val(),
        name:$('[name=name]').val(),
        age:$('[name=age]').val()
    }
    if(obj.id){
        putProduct(obj.id,obj).then(res=>{
            if(res.code===1000){
                location.href = './index.html'
            }else{
                alert(code.msg)
            }
        })
    }else{
        postProduct(obj).then(data=>{
            if(data.code==1000){
                location.href = './index.html'
            }else{
                alert(code.msg)
            }
        })
    }
}

function cancel(){
    location.href = './index.html'
}